// 闭包可以作为函数的参数传递。Rust 提供了几种 trait 来表示闭包的类型：
//
// Fn: 不可变借用捕获环境变量。
fn apply<F>(f: F)
where
    F: Fn(),
{
    f();
}
#[test]
fn test_fn() {
    let x = 5;
    let print_x = || println!("x={}", x);
    apply(print_x);
}
// FnMut
fn apply_mut<F>(mut f: F)
where
    F: FnMut(),
{
  f();
}
#[test]
fn test_fn_mut(){
    let mut x = 5;
    let add_x = || {
        x+=1;
        println!("x={}", x);
    };
    apply_mut(add_x);
}
// FnOnce
fn apply_once<F>(f:F)
where F:FnOnce(),
{
    f();
}
#[test]
fn test_fn_once(){
    let x = vec![1, 2, 3];
    let consume_x = ||{
        println!("Consuming x : {:?}", x);
    };
    apply_once(consume_x);
}